import enterprises.TypeConsumer;
import enterprises.TypeOwnership;
import enterprises.TypeProvider;
import enterprises.IndustrialSector;
import enterprises.Enterprise;
import enterprises.Production;
import enterprises.Consumer;
import enterprises.Provider;
import enterprises.User;
import enterprises.Role;
import enterprises.UserRole;

class BootStrap {
	
	def springSecurityService

	def init = { servletContext ->
		def machineProvider;
		def boilerProvider;
		def steelProvider;
		def transistorProvider;
		def processorsProvider;
		def diodeProvider;
		def tyreProvider;
		def glassProvider;
		def alloyProvider;


		def automobileTrader;
		def tractorProducer;
		def infoPro;
		def epam;


		def localTypeConsumer;
		def RfTypeConsumer;
		def sngTypeConsumer;
		def foreignTypeConsumer;

		def localTypeProvider;
		def RfTypeProvider;
		def sngTypeProvider;
		def foreignTypeProvider;

		def lightSector;
		def heavySector;
		def machineBuildingSector;
		def metallurgySector;
		def itSector;

		def stateOwnership;
		def municipalityOwnership;
		def privateOwnership;
		def jointStockOwnership;

		def metallurgyFactory;
		def cisco;
		def carFactory;
		def machineFactory;

		def machineProduction;
		def automobileProduction;
		def steelProduction;
		def ironProduction;
		def copperProduction;
		def routerProduction;
		def hubProduction;
		def switchProduction;

		if (!TypeConsumer.count()) {
			localTypeConsumer = new TypeConsumer(name:"Local");
			RfTypeConsumer = new TypeConsumer(name:"Russian Federation");
			sngTypeConsumer = new TypeConsumer(name:"SNG");
			foreignTypeConsumer = new TypeConsumer(name:"Foreign");

			localTypeConsumer.save();
			RfTypeConsumer.save();
			sngTypeConsumer.save();
			foreignTypeConsumer.save();
		}
		if (!TypeOwnership.count()) {
			stateOwnership = new TypeOwnership(name:"State");
			municipalityOwnership = new TypeOwnership(name:"Municipality");
			privateOwnership = new TypeOwnership(name:"Private");
			jointStockOwnership = new TypeOwnership(name:"Joint-stock");
			stateOwnership.save();
			municipalityOwnership.save();
			privateOwnership.save();
			jointStockOwnership.save();
		}
		if (!TypeProvider.count()) {
			localTypeProvider = new TypeProvider(name:"Local");
			RfTypeProvider = new TypeProvider(name:"Russian Federation");
			sngTypeProvider = new TypeProvider(name:"SNG");
			foreignTypeProvider = new TypeProvider(name:"Foreign");

			localTypeProvider.save();
			RfTypeProvider.save();
			sngTypeProvider.save();
			foreignTypeProvider.save();
		}
		if (!IndustrialSector.count()) {
			lightSector = new IndustrialSector(name:"Light");
			heavySector = new IndustrialSector(name:"Heavy");
			machineBuildingSector = new IndustrialSector(name:"Machine Building");
			metallurgySector = new IndustrialSector(name:"Metallurgy");
			itSector = new IndustrialSector(name:"Information Technology");
			lightSector.save();
			heavySector.save();
			machineBuildingSector.save();
			metallurgySector.save();
			itSector.save();
		}
		if (!Enterprise.count()) {
			metallurgyFactory = new Enterprise(name:"Metallurgy Factory",
					registrationNumber:1,
					industrialSector:metallurgySector,
					typeOwnership:stateOwnership);
			cisco = new Enterprise(name:"Cisco Company",
					registrationNumber:2,
					industrialSector:itSector,
					typeOwnership:jointStockOwnership);
			carFactory = new Enterprise(name:"Car Factory",
					registrationNumber:3,
					industrialSector:machineBuildingSector,
					typeOwnership:privateOwnership);
			machineFactory = new Enterprise(name:"Machinery Factory",
					registrationNumber:4,
					industrialSector:machineBuildingSector,
					typeOwnership:municipalityOwnership);
			metallurgyFactory.save();
			cisco.save();
			carFactory.save();
			machineFactory.save();
		}
		if (!Production.count()) {
			automobileProduction = new Production(name:"Automobile",
					monthlySize:300);
			steelProduction = new Production(name:"Steel",
					monthlySize:50);
			ironProduction = new Production(name:"Iron",
					monthlySize:10);
			copperProduction = new Production(name:"Copper",
					monthlySize:20);
			routerProduction = new Production(name:"Router",
					monthlySize:40);
			hubProduction = new Production(name:"Hub",
					monthlySize:60);
			switchProduction = new Production(name:"Switch",
					monthlySize:70);

			carFactory.addToProductions(automobileProduction);
			metallurgyFactory.addToProductions(steelProduction);
			metallurgyFactory.addToProductions(ironProduction);
			metallurgyFactory.addToProductions(copperProduction);
			cisco.addToProductions(routerProduction);
			cisco.addToProductions(hubProduction);
			cisco.addToProductions(switchProduction);
		}
		if (!Consumer.count()) {
			automobileTrader = new Consumer(name:"Automobile Trader",
					typeConsumer:RfTypeConsumer);
			tractorProducer = new Consumer(name:"Tractor Producer",
					typeConsumer:sngTypeConsumer);
			infoPro = new Consumer(name:"Info Pro",
					typeConsumer:localTypeConsumer);
			epam = new Consumer(name:"EPAM",
					typeConsumer:foreignTypeConsumer);
			automobileTrader.addToProductions(automobileProduction);
			tractorProducer.addToProductions(steelProduction);
			tractorProducer.addToProductions(automobileProduction);
			tractorProducer.addToProductions(ironProduction);
			tractorProducer.addToProductions(copperProduction);
			infoPro.addToProductions(routerProduction);
			infoPro.addToProductions(switchProduction);
			infoPro.addToProductions(hubProduction);
			infoPro.addToProductions(automobileProduction);
			infoPro.addToProductions(steelProduction);
			epam.addToProductions(routerProduction);
			epam.addToProductions(switchProduction);
			epam.addToProductions(hubProduction);
			epam.addToProductions(ironProduction);

			automobileTrader.save();
			tractorProducer.save();
			infoPro.save();
			epam.save();
		}
		if (!Provider.count()) {
			machineProvider = new Provider(name:"Machine Provider",
					primaryProduct:"Machine",
					typeProvider:localTypeProvider
					);
			boilerProvider = new Provider(name:"Boiler Provider",
					primaryProduct:"Boiler",
					typeProvider:localTypeProvider);
			steelProvider = new Provider(name:"Steel Provider",
					primaryProduct:"Steel",
					typeProvider:RfTypeProvider);
			transistorProvider = new Provider(name:"Transistor Provider",
					primaryProduct:"Transistor",
					typeProvider:foreignTypeProvider);
			processorsProvider = new Provider(name:"Processor Provider",
					primaryProduct:"Processor",
					typeProvider:foreignTypeProvider);
			diodeProvider = new Provider(name:"Diode Provider",
					primaryProduct:"Diode",
					typeProvider:foreignTypeProvider);
			tyreProvider = new Provider(name:"Tyre Provider",
					primaryProduct:"Tyre",
					typeProvider:sngTypeProvider);
			glassProvider = new Provider(name:"Glass Provider",
					primaryProduct:"Glass",
					typeProvider:sngTypeProvider);
			alloyProvider = new Provider(name:"Alloy Provider",
					primaryProduct:"Alloy",
					typeProvider:RfTypeProvider);
			machineProvider.addToEnterprises(metallurgyFactory);
			boilerProvider.addToEnterprises(metallurgyFactory);
			steelProvider.addToEnterprises(machineFactory);
			tyreProvider.addToEnterprises(carFactory);
			glassProvider.addToEnterprises(carFactory);
			alloyProvider.addToEnterprises(carFactory);
			transistorProvider.addToEnterprises(cisco);
			processorsProvider.addToEnterprises(cisco);
			diodeProvider.addToEnterprises(cisco);

			machineProvider.save();
			boilerProvider.save();
			steelProvider.save();
			transistorProvider.save();
			processorsProvider.save();
			diodeProvider.save();
			tyreProvider.save();
			glassProvider.save();
			alloyProvider.save();
		}

		def usualUser = new User(username:"user", password:springSecurityService.encodePassword("user"), enabled:true);
		def usualRole = new Role(authority:"ROLE_USER");
		usualUser.save();
		usualRole.save();
		UserRole.create(usualUser, usualRole, true);
		
		def admin = new User(username:"admin", password:springSecurityService.encodePassword("admin"), enabled:true);
		def adminRole = new Role(authority:"ROLE_ADMIN");
		admin.save();
		adminRole.save();
		UserRole.create(admin, adminRole, true);		
	}
	def destroy = {
	}
}
